package collection.set;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @author weim
 * @date 18-8-27
 *
 * 实现就是个 TreeMap
 * key值 是添加的值
 * value 是个固定的值 private static final Object PRESENT = new Object();
 *
 * 非线程安全
 *
 */
public class MyTreeSet {

    public static void main(String[] args) {
        TreeSet set = new TreeSet();
        set.add("a");
        set.add("e");
        set.add("b");
        set.add("c");
        set.add("d");
        //不允许为null  因为TreeMap在put是不允许key为null
//        set.add(null);
        //可以为""
        set.add("");

        System.out.println(set.toString());


        TreeSet set1 = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof String && o2 instanceof String) {
                    return ((String) o1).compareTo(o2.toString());
                }
                return -1;
            }
        });

        set1.add("a");
        set1.add("e");
        set1.add("b");
        set1.add("c");
        set1.add("d");
        set1.add(null);

        System.out.println(set1.toString());



    }

}
